package com.cloudptt.wp.ui.message;

import java.util.ArrayList;
import java.util.List;

//import com.amap.api.mapcore.util.bu;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ui.IAddrUpdate;
import com.cloudptt.wp.ui.message.adapter.HistoryAdapter;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.HistoryInfo;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

public class MessageFragment extends Fragment implements OnItemClickListener,OnItemLongClickListener{
	private static final String THIS_FILE = "MessageFragment";
	public static final String KEY = "addrInfo";
	private Context ctx = null;
	private AddrInfo mcIdInfo = null;
	private IMsgCallBack callBack = null;
	private View mRootView = null;
	private ListView listView = null;
	private List<HistoryInfo> list;
	private HistoryAdapter mAdapter;
	private IAddrUpdate update = null;
	
	
	public MessageFragment() {
		super();
	}

	public MessageFragment(Context ctx,AddrInfo mcIdInfo,IMsgCallBack callBack,IAddrUpdate update) {
		// TODO Auto-generated constructor stub
		this.ctx = ctx;
		this.mcIdInfo = mcIdInfo;
		this.callBack = callBack;
		this.update = update;
		list = new ArrayList<HistoryInfo>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(mRootView != null){
			return mRootView;
		}
		mRootView = inflater.inflate(R.layout.sms_page, container, false);
		listView = (ListView) mRootView.findViewById(R.id.msgListView);
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);
		AddrInfo info = null;
		if(update != null)
		{
			info = update.getCurGroupInfo();
			Log.d(THIS_FILE, "updateHistoryRecords defaultGroupInfo = "+info);
		}
		mAdapter = new HistoryAdapter(ctx,list,info);
		listView.setAdapter(mAdapter);
		return mRootView;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(THIS_FILE, "onResume");
		if(callBack !=null){
			callBack.updateHistoryInfoList();
		}
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		HistoryInfo info = (HistoryInfo)listView.getAdapter().getItem(position);
		Log.debug(this.getClass(), "onItemClick info = " + info);
		AddrInfo addrInfo = info.getPeerAddrInfo();
		Intent intent = new Intent(ctx,SessionActivity.class);
		intent.putExtra(SessionActivity.KEY, addrInfo);
		startActivity(intent);	
		getActivity().overridePendingTransition( R.anim.in_from_right, R.anim.out_to_left );
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		final HistoryInfo info = (HistoryInfo)listView.getAdapter().getItem(position);
		//		showContactDialog(new String[]{getString(R.string.delete)}, info);
		AddrInfo addrInfo = info.getPeerAddrInfo();
		final boolean isGroup = addrInfo.isGroupType();
		String tipStr = getString(R.string.del_user_history_records);
		if(isGroup)
		{
			tipStr = getString(R.string.del_group_history_records);
		}
		
		DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				boolean isSuc = callBack.delHirstoryRecord(info);
				if(isSuc)
				{
					callBack.updateHistoryInfoList();
				}
			}
		};
		
		DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				
			}
		};
		CommDlg.showConfirmDlg(ctx, tipStr, okListener, cancelListener);
		return true;
	}

	public void updateHistoryRecords(List<HistoryInfo> list) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE, "updateHistoryRecords list = " + list);
		this.list = list;
		if(mAdapter != null
				&& update != null)
		{
			AddrInfo info = update.getCurGroupInfo();
			Log.d(THIS_FILE, "updateHistoryRecords info = "+info);
			mAdapter.updateData(list,info);
		}
	}
}
